"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.符战师Handler = void 0;
const Config_1 = require("../../Config");
const Big_1 = require("../Big");
const ___1 = require("../evolution/\u7B26\u6218\u5E08");
const Point_1 = require("../Point");
const Utils_1 = __importDefault(require("../Utils"));
class 符战师Handler {
    constructor(gameMgr, handlerMgr) {
        this.gameMgr = gameMgr;
        this.handlerMgr = handlerMgr;
    }
    gc() {
        this.gameMgr = null;
        this.handlerMgr = null;
    }
    move(data, player) {
        this.handlerMgr.spendAction(player, Config_1.Config.COST_符战师_MOVE);
        let startPoint = this.gameMgr.map_point.get(data.start);
        let endPoint = this.gameMgr.map_point.get(data.end);
        let big_start = startPoint.big;
        let has_hide = startPoint.terrain == Point_1.Terrain.LIN && big_start.arr_states.has(Big_1.BigState.HIDE);
        let big_end = endPoint.big;
        if (big_end != null) {
            this.handlerMgr.fail(player, "移动失败");
            big_end.deleteState(Big_1.BigState.HIDE);
            big_start.die();
            return;
        }
        startPoint.big = null;
        endPoint.big = big_start;
        big_start.point = endPoint;
        big_start.deleteState(Big_1.BigState.HIDE);
        big_start.deleteState(Big_1.BigState.BURDEN);
        if (endPoint.terrain == Point_1.Terrain.LIN) {
            big_start.addState(Big_1.BigState.HIDE);
        }
        else if (endPoint.terrain == Point_1.Terrain.SHA) {
            big_start.addState(Big_1.BigState.BURDEN);
        }
        //下一个地形不是林，自己是潜行的
        let ts = big_start.evolutionTs;
        if (ts.arr_spell.has(___1.Spell.隐迹) && endPoint.terrain != Point_1.Terrain.LIN && has_hide) {
            big_start.addState(Big_1.BigState.HIDE);
        }
        this.gameMgr.sendBroadcast(Utils_1.default.getObj("big_move", { "start": data.start, "end": data.end }));
    }
    attack(data, player) {
        let cost = Config_1.Config.COST_符战师_ATTACK;
        if (___1.Spell.善战) {
            cost--;
        }
        this.handlerMgr.spendAction(player, cost);
        let startPoint = this.gameMgr.map_point.get(data.start);
        let endPoint = this.gameMgr.map_point.get(data.end);
        let big_start = startPoint.big;
        big_start.deleteState(Big_1.BigState.HIDE);
        let big_end = endPoint.big;
        let small = endPoint.small;
        let big_end_father;
        let is_killed = false;
        if (endPoint.terrain == Point_1.Terrain.JING) {
            //水晶被攻击
            this.gameMgr.gameOver(player);
        }
        else if (small != null) {
            //小子被攻击
            small.die();
        }
        else if (big_end != null) {
            is_killed = true;
            big_end_father = big_end.father;
            //大子被攻击
            if (big_end.attacked()) {
                //打过了
            }
            else {
                //攻击没打过，自己死亡
                big_start.die();
                return;
            }
        }
        startPoint.big = null;
        endPoint.big = big_start;
        big_start.point = endPoint;
        this.gameMgr.sendBroadcast(Utils_1.default.getObj("big_move", { "start": data.start, "end": data.end }));
        //符咒效果
        let ts = big_start.evolutionTs;
        ts.arr_spell.forEach(spell => {
            switch (spell) {
                case ___1.Spell.常胜:
                    if (is_killed) {
                        big_start.addState(Big_1.BigState.SHIELD);
                    }
                    break;
                case ___1.Spell.嗜血:
                    player.small.action++;
                    break;
                case ___1.Spell.怒威:
                    if (big_end_father != null) {
                        big_end_father.addState(Big_1.BigState.FEAR);
                    }
                    let arr_points = Utils_1.default.getArrPointsAround(player.gameMgr, endPoint);
                    arr_points.forEach(point => {
                        let big = point.big;
                        if (big != null) {
                            if (big.player != player) {
                                big.addState(Big_1.BigState.FEAR);
                            }
                        }
                    });
                    break;
                case ___1.Spell.火拳:
                    let arr = Utils_1.default.getArrPointsAround(player.gameMgr, endPoint);
                    arr.push(endPoint);
                    arr.forEach(point => {
                        let big = point.big;
                        if (big != null) {
                            if (big.player != player) {
                                big.attacked();
                            }
                        }
                        if (point.terrain == Point_1.Terrain.LIN) {
                            point.changeTerrain(Point_1.Terrain.KONG);
                        }
                    });
                    break;
                case ___1.Spell.连战:
                    big_start.is_ready = true;
            }
        });
    }
    /** 刻符 */
    carve(data, player) {
        this.handlerMgr.spendAction(player, data.cost);
        let point = this.gameMgr.map_point.get(data.id);
        let big = point.big;
        let ts = big.evolutionTs;
        ts.arr_spell.add(data.spell);
        this.gameMgr.sendBroadcast(Utils_1.default.getObj("符战师_carve", { "username": player.username, "id": big.id, "spell": data.spell }));
        if (data.spell == ___1.Spell.神行) {
            ts.step = 2;
        }
    }
}
exports.符战师Handler = 符战师Handler;
